package com.lagou.sort;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * @author lcy
 * @description 排序sortReducer
 * @date 2021/11/6 23:35
 * @vserion 1.3.3
 */
public class SortReducer extends Reducer<LongWritable, LongWritable, LongWritable, Text> {
	// 设置行数初始值
	private static LongWritable lineCount = new LongWritable(1);
	// 设置带制表符的value
	private static Text text = new Text();

	@Override
	protected void reduce(LongWritable key, Iterable<LongWritable> values, Reducer<LongWritable, LongWritable, LongWritable, Text>.Context context) throws IOException, InterruptedException {
		// 在这里只需要根据values进行遍历就可以得到完整的KEY
		// 而KEY正好是读取到的文本的内容。
		for (LongWritable value : values) {
			text.set("\t"+key);
			context.write(lineCount, text);
			// 这里只需要在reducer端进行一个行数的递增即可。
			lineCount = new LongWritable(lineCount.get()+1);
		}
	}
}
